package com.mzj.saas.rent;

import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.core.BaseController;
import com.haixiaoke.saas.repository.core.page.TableDataInfo;
import com.haixiaoke.saas.repository.pojo.OpLead;
import com.haixiaoke.saas.repository.service.IOpLeadService;
import com.mzj.saas.constant.OpConstant;
import com.mzj.saas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 公众号留咨信息Controller
 *
 * @author qixi
 * @date 2024-01-16
 */
@RestController
@RequestMapping("/lead")
public class OpLeadController extends BaseController {

    @Autowired
    private IOpLeadService opLeadService;
    @Autowired
    private LoginUserUtil loginUserUtil;

    /**
     * 查询公众号留咨信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OpLead opLead) {
        startPage();
        Integer loginType = loginUserUtil.getLoginType();
        if (loginType == 1) {
            opLead.setOrgId(loginUserUtil.getOrgId());
        }
        List<OpLead> list = opLeadService.selectOpLeadList(opLead);
        return getDataTable(list);
    }

    /**
     * 获取公众号留咨信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(opLeadService.selectOpLeadById(id));
    }

    /**
     * 新增公众号留咨信息
     */

    @PostMapping
    public AjaxResult add(@RequestBody OpLead opLead) {
        Integer loginType = loginUserUtil.getLoginType();
        if (loginType == 1) {
            opLead.setOrgId(loginUserUtil.getOrgId());
        }
        return toAjax(opLeadService.insertOpLead(opLead));
    }

    /**
     * 修改公众号留咨信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody OpLead opLead) {
        opLead.setHandleBy(loginUserUtil.getId());
        return toAjax(opLeadService.updateOpLead(opLead));
    }

    /**
     * 修改公众号留咨信息
     */
    @GetMapping("/followUp/{id}")
    public AjaxResult followUp(@PathVariable Long id) {
        OpLead opLead = opLeadService.selectOpLeadById(id);
        if (ObjectUtil.isNull(opLead)) {
            throw new RuntimeException("数据不存在");
        }

        if (!opLead.getHandleStatus().equals(OpConstant.LEAD_STATUS_UNTREATED)) {
            throw new RuntimeException("留咨信息已跟进");
        }

        opLead.setHandleStatus(OpConstant.LEAD_STATUS_PROCESSED);
        opLead.setHandleBy(loginUserUtil.getId());
        return toAjax(opLeadService.updateOpLead(opLead));
    }

    /**
     * 删除公众号留咨信息
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(opLeadService.deleteOpLeadByIds(ids));
    }
}
